<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Index and search analysis | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'analysis-index-search-time.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-index-search-time.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/analysis-index-search-time.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/analysis-index-search-time.html" rel="nofollow" target="_blank">../en/analysis-index-search-time.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="analysis.html">Text analysis</a></span>
»
<span class="breadcrumb-link"><a href="analysis-concepts.html">Text analysis concepts</a></span>
»
<span class="breadcrumb-node">Index and search analysis</span>
</div>
<div class="navheader">
<span class="prev">
<a href="analyzer-anatomy.html">« Anatomy of an analyzer</a>
</span>
<span class="next">
<a href="stemming.html">Stemming »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="analysis-index-search-time"></a>Index and search analysis<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/index-search-time.asciidoc">edit</a>
</h2>
</div></div></div>
<p>Text analysis occurs at two times:</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
Index time
</span>
</dt>
<dd>
When a document is indexed, any <a class="xref" href="text.html" title="Text datatype"><code class="literal">text</code></a> field values are analyzed.
</dd>
<dt>
<span class="term">
Search time
</span>
</dt>
<dd>
<p>
When running a <a class="xref" href="full-text-queries.html" title="Full text queries">full-text search</a> on a <code class="literal">text</code> field,
the query string (the text the user is searching for) is analyzed.
</p>
<p>Search time is also called <em>query time</em>.</p>
</dd>
</dl>
</div>
<p>The analyzer, or set of analysis rules, used at each time is called the <em>index
analyzer</em> or <em>search analyzer</em> respectively.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="analysis-same-index-search-analyzer"></a>How the index and search analyzer work together<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/index-search-time.asciidoc">edit</a>
</h3>
</div></div></div>
<p>In most cases, the same analyzer should be used at index and search time. This
ensures the values and query strings for a field are changed into the same form
of tokens. In turn, this ensures the tokens match as expected during a search.</p>
<details>
<summary class="title"><span class="strong strong"><strong>Example</strong></span></summary>
<div class="content">
<p>A document is indexed with the following value in a <code class="literal">text</code> field:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">The QUICK brown foxes jumped over the dog!</pre>
</div>
<p>The index analyzer for the field converts the value into tokens and normalizes
them. In this case, each of the tokens represents a word:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ quick, brown, fox, jump, over, dog ]</pre>
</div>
<p>These tokens are then indexed.</p>
<p>Later, a user searches the same <code class="literal">text</code> field for:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">"Quick fox"</pre>
</div>
<p>The user expects this search to match the sentence indexed earlier,
<code class="literal">The QUICK brown foxes jumped over the dog!</code>.</p>
<p>However, the query string does not contain the exact words used in the
document’s original text:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">quick</code> vs <code class="literal">QUICK</code>
</li>
<li class="listitem">
<code class="literal">fox</code> vs <code class="literal">foxes</code>
</li>
</ul>
</div>
<p>To account for this, the query string is analyzed using the same analyzer. This
analyzer produces the following tokens:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ quick, fox ]</pre>
</div>
<p>To execute the search, Elasticsearch compares these query string tokens to the tokens
indexed in the <code class="literal">text</code> field.</p>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Token</th>
<th align="left" valign="top">Query string</th>
<th align="left" valign="top">
<code class="literal">text</code> field</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">quick</code></p></td>
<td align="left" valign="top"><p>X</p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">brown</code></p></td>
<td align="left" valign="top"><p></p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">fox</code></p></td>
<td align="left" valign="top"><p>X</p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">jump</code></p></td>
<td align="left" valign="top"><p></p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">over</code></p></td>
<td align="left" valign="top"><p></p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">dog</code></p></td>
<td align="left" valign="top"><p></p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
</tbody>
</table>
</div>
<p>Because the field value are query string were analyzed in the same way, they
created similar tokens. The tokens <code class="literal">quick</code> and <code class="literal">fox</code> are exact matches. This
means the search matches the document containing <code class="literal">"The QUICK brown foxes jumped
over the dog!"</code>, just as the user expects.</p>
</div>
</details>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="different-analyzers"></a>When to use a different search analyzer<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/analysis/index-search-time.asciidoc">edit</a>
</h3>
</div></div></div>
<p>While less common, it sometimes makes sense to use different analyzers at index
and search time. To enable this, Elasticsearch allows you to
<a class="xref" href="specify-analyzer.html#specify-search-analyzer" title="How Elasticsearch determines the search analyzer">specify a separate search analyzer</a>.</p>
<p>Generally, a separate search analyzer should only be specified when using the
same form of tokens for field values and query strings would create unexpected
or irrelevant search matches.</p>
<details id="different-analyzer-ex">
<summary class="title"><span class="strong strong"><strong>Example</strong></span></summary>
<div class="content">
<p>Elasticsearch is used to create a search engine that matches only words that start with
a provided prefix. For instance, a search for <code class="literal">tr</code> should return <code class="literal">tram</code> or
<code class="literal">trope</code>—but never <code class="literal">taxi</code> or <code class="literal">bat</code>.</p>
<p>A document is added to the search engine’s index; this document contains one
such word in a <code class="literal">text</code> field:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">"Apple"</pre>
</div>
<p>The index analyzer for the field converts the value into tokens and normalizes
them. In this case, each of the tokens represents a potential prefix for
the word:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ a, ap, app, appl, apple]</pre>
</div>
<p>These tokens are then indexed.</p>
<p>Later, a user searches the same <code class="literal">text</code> field for:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">"appli"</pre>
</div>
<p>The user expects this search to match only words that start with <code class="literal">appli</code>,
such as <code class="literal">appliance</code> or <code class="literal">application</code>. The search should not match <code class="literal">apple</code>.</p>
<p>However, if the index analyzer is used to analyze this query string, it would
produce the following tokens:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ a, ap, app, appl, appli ]</pre>
</div>
<p>When Elasticsearch compares these query string tokens to the ones indexed for <code class="literal">apple</code>,
it finds several matches.</p>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Token</th>
<th align="left" valign="top"><code class="literal">appli</code></th>
<th align="left" valign="top"><code class="literal">apple</code></th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">a</code></p></td>
<td align="left" valign="top"><p>X</p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ap</code></p></td>
<td align="left" valign="top"><p>X</p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">app</code></p></td>
<td align="left" valign="top"><p>X</p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">appl</code></p></td>
<td align="left" valign="top"><p>X</p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">appli</code></p></td>
<td align="left" valign="top"><p></p></td>
<td align="left" valign="top"><p>X</p></td>
</tr>
</tbody>
</table>
</div>
<p>This means the search would erroneously match <code class="literal">apple</code>. Not only that, it would
match any word starting with <code class="literal">a</code>.</p>
<p>To fix this, you can specify a different search analyzer for query strings used
on the <code class="literal">text</code> field.</p>
<p>In this case, you could specify a search analyzer that produces a single token
rather than a set of prefixes:</p>
<div class="pre_wrapper lang-text">
<pre class="programlisting prettyprint lang-text">[ appli ]</pre>
</div>
<p>This query string token would only match tokens for words that start with
<code class="literal">appli</code>, which better aligns with the user’s search expectations.</p>
</div>
</details>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="analyzer-anatomy.html">« Anatomy of an analyzer</a>
</span>
<span class="next">
<a href="stemming.html">Stemming »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>